/*
 * @(#)UICityServiceImp.java
 * Copyright (c) 2011 TravelSky Technology Ltd. All Right Reserved. 
 */
package com.meetingshow.meeting.uiservice;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.meetingshow.common.base.service.model.QueryResults;
import com.meetingshow.common.base.uiservice.BaseUIServiceImp;
import com.meetingshow.common.base.uiservice.exception.BaseUIServiceException;
import com.meetingshow.common.validator.RemoveValidator;
import com.meetingshow.common.validator.SaveValidator;
import com.meetingshow.common.validator.UpdateValidator;
import com.meetingshow.meeting.dao.model.CityPO;
import com.meetingshow.meeting.service.ICityService;
import com.meetingshow.meeting.service.model.CityQueryParas;
import com.meetingshow.meeting.uiservice.exception.CityNotExistException;
import com.meetingshow.meeting.uiservice.model.CityQueryParasVO;
import com.meetingshow.meeting.uiservice.model.CityVO;

public class UICityServiceImp extends BaseUIServiceImp implements IUICityService {

    private final static Logger logger = Logger.getLogger(UICityServiceImp.class);
    
    private ICityService cityService;

    public ICityService getCityService() {
        return cityService;
    }

    public void setCityService(ICityService cityService) {
        this.cityService = cityService;
    }

	@Override
	public CityVO getCityById (java.lang.Integer cityId) throws BaseUIServiceException {
	    if (cityId == null) {
	        this.throwValidaterException("cityId is null.");
	    }
	    
	    CityPO cityPO = null;
	    try {
	        cityPO = getCityService().getCityById(cityId);
	    } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
	    }
	    
	    if (cityPO != null) {
	        return new CityVO(cityPO);
	    } else {
	        return null;
	    }
	}

    @Override
    public QueryResults<CityVO> queryCity(CityQueryParasVO cityQueryParasVO) throws BaseUIServiceException {
        
        if (cityQueryParasVO == null) {
            this.throwValidaterException("cityQueryParasVO is null.");
        }
        
        validateThrowEx(cityQueryParasVO);
        
        QueryResults<CityVO> cityVOQueryResults = new QueryResults<CityVO>();
        List<CityVO> cityVOList = new ArrayList<CityVO>();
        QueryResults<CityPO> cityPOQueryResults = null;
        
        try {
            cityPOQueryResults = getCityService().queryCity(cityQueryParasVO.toCityQueryParas());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        if (QueryResults.resultsListNotNull(cityPOQueryResults)) {
            for (CityPO cityPO : cityPOQueryResults.getResultList()) {
                if (cityPO != null) {
                    cityVOList.add(new CityVO(cityPO));
                }
            }
        }
        
        cityVOQueryResults.setResultList(cityVOList);
        cityVOQueryResults.setFirstResultNo(cityPOQueryResults.getFirstResultNo());
        cityVOQueryResults.setResultsPerGroup(cityPOQueryResults.getResultsPerGroup());
        cityVOQueryResults.setTotalResultsNum(cityPOQueryResults.getTotalResultsNum());
        
        return cityVOQueryResults;
    }

    @Override
    public void removeCity(CityVO cityVO)
            throws BaseUIServiceException {
        
        if (cityVO == null) {
            this.throwValidaterException("cityVO is null");
        }
        
        validateThrowEx(cityVO, RemoveValidator.class);
        
        CityQueryParas cityQueryParas = new CityQueryParas();
        cityQueryParas.setCityId_e(String.valueOf(cityVO.getCityId()));
        QueryResults<CityPO> cityQueryResults = null;
        try {
            cityQueryResults = getCityService().queryCity(cityQueryParas);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        if (!QueryResults.resultsListNotNull(cityQueryResults)) {
            throw new CityNotExistException();
        }
        
        CityPO cityPO = cityQueryResults.getResultList().get(0);
        try {
            getCityService().removeCity(cityPO);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
    }
    
    @Override
    public java.lang.Integer saveCity(CityVO cityVO)
            throws BaseUIServiceException {
        if (cityVO == null) {
            this.throwValidaterException("cityVO is null");
        }
        
        cityVO.setCreateTime(new Date());
        
        validateThrowEx(cityVO, SaveValidator.class);
        
        CityPO cityPO = cityVO.toCityPO();
        try {
            getCityService().saveCity(cityPO);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        return cityPO.getCityId();
    }

    @Override
    public void updateCity(CityVO cityVO)
            throws BaseUIServiceException {
        if (cityVO == null) {
            this.throwValidaterException("cityVO is null");
        }
        
        validateThrowEx(cityVO, UpdateValidator.class);
        
		CityQueryParas cityQueryParas = new CityQueryParas();
        cityQueryParas.setCityId_e(String.valueOf(cityVO.getCityId()));
        QueryResults<CityPO> cityQueryResults = null;
        try {
            cityQueryResults = getCityService().queryCity(cityQueryParas);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        if (!QueryResults.resultsListNotNull(cityQueryResults)) {
            throw new CityNotExistException();
        }
        
        CityPO oldCityPO = cityQueryResults.getResultList().get(0);
        
        oldCityPO.setAbbrev(cityVO.getAbbrev());
        oldCityPO.setCreateTime(cityVO.getCreateTime());
        oldCityPO.setName(cityVO.getName());
        oldCityPO.setNameEn(cityVO.getNameEn());
        
        try {
            getCityService().updateCity(oldCityPO);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
    }
    
    @Override
    public List<CityVO> getAllCities() throws BaseUIServiceException {
    	List<CityVO> cities = new ArrayList<CityVO>();
    	
    	CityQueryParas cityQueryParas = new CityQueryParas();
    	QueryResults<CityPO> cityQueryResults = null;
    	try {
    		cityQueryResults = getCityService().queryCity(cityQueryParas);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseUIServiceException(e);
		}
		
		if (QueryResults.resultsListNotNull(cityQueryResults)) {
			for (CityPO cityPO : cityQueryResults.getResultList()) {
				if (cityPO != null) {
					cities.add(new CityVO(cityPO));
				}
			}
		}
    	
    	return cities;
    }
}